import { asyncRoutes } from '@/router'
export function convertRouter(routers) {
  return routers.map(route => {
    return setRoutes(route, asyncRoutes)
  })
}

/**
 * @description 处理后端路由数据
 * @param {*} route 路由数据
 * @param {*} list 前端路由 asyncRoutes配置项
 * @returns {*}
 */
const setRoutes = (route, list) => {
  list.filter(item => {
    if (item.path === route.path) {
      route.component = item.component
      route.meta = item.meta
      route.name = item.name
      if (route.children && route.children.length) {
        const children = []
        route.children.filter(option => {
          children.push(setRoutes(option, item.children))
        })
        route.children = children
      }
    }
  })
  return route
}

// 判断用户权限组是否和routes中meta规定的路由相同
function hasPermission(permissions, route) {
  if (route.meta && route.meta.rightList) {
    return permissions.some(role => route.meta.rightList.includes(role))
  } else {
    return true
  }
}
// 过滤路由
export function filterAsyncRoutes(routes, permissions) {
  const finallyRoutes = []
  routes.forEach(route => {
    const item = { ...route }
    if (hasPermission(permissions, item)) {
      if (item.children) {
        item.children = filterAsyncRoutes(item.children, permissions)
      }
      finallyRoutes.push(item)
    }
  })
  return finallyRoutes
}
